var commonUtil = require("../../../utils/commonUtil.js")
var globalData = getApp().globalData;
var httpsUrl = globalData.httpsUrl;
var imgUrl = globalData.imgUrl;
Page({

  data: {
    imgUrl: imgUrl,
    titleFlag: -1,// 1,待支付 2,待使用 3,待评价 4,已完成 5,待发货 6,待收货
    list:[],
    pageNumber:1,
    pageSize:10,
    totalPage:1,
    //type类型，1商品 2卡 3充值
  },

  list:function(){
    var that = this;
    var params = new Object();
    params.pageNumber = this.data.pageNumber;
    params.pageSize = this.data.pageSize;
    let status = this.data.titleFlag;
    if (status != -1) {
      params.status = Number(status);
    }
    console.log("params:" + JSON.stringify(params));
    let openId = wx.getStorageSync("openidKey");
    commonUtil.showLoading("加载中...");
    wx.request({
      url: httpsUrl + "/order/list",
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      // dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        res = res.data;
        console.log("code:" + JSON.stringify(res.result));
        console.log("res:" + JSON.stringify(res));
        if (res.result == 0) {
          var rows = res.data.list;
          if (rows != null && rows.length > 0) {
            for (let i = 0; i < rows.length; i++) {
              if (!commonUtil.isEmpty(rows[i].receivable)) {
                rows[i].receivable = rows[i].receivable.toFixed(2);
              }
              //1,待支付 2,待使用 3,待评价 4,已完成 5,待发货 6,待收货
              if (rows[i].status == 1) { rows[i].statusstr = '待支付' }
              else if (rows[i].status == 2) { rows[i].statusstr = '待使用' }
              else if (rows[i].status == 3) { rows[i].statusstr = '待评价' }
              else if (rows[i].status == 4) { rows[i].statusstr = '已完成' }
              else if (rows[i].status == 5) { rows[i].statusstr = '待发货' }
              else if (rows[i].status == 6) { rows[i].statusstr = '待收货' }
            }
          }
          that.setData({ totalPage: res.data.totalPage })
          if (that.data.pageNumber == 1) {
            that.setData({ list: rows })
          } else {
            var list = that.data.list;
            list = list.concat(rows);
            that.setData({ list: list })
          }
        } else {
          // commonUtil.showModal("订单加载失败，请稍候重试：" + res.msg);
        }
      }
    })
  },
  //设置显示块id
  titleSelect: function (e) {
    let id = e.currentTarget.id;
    this.setData({
      titleFlag: id,
    })
    this.list();
  },
  info:function(e){
    let id = e.currentTarget.id;
    let list = this.data.list;
    let row = list[id];
    wx.setStorageSync("orderInfo", row);
    wx.navigateTo({
      url: '../orderInfo/orderInfo',
    })
  },
  //物流信息
  logistics: function (e) {
    let id = e.currentTarget.id;
    let list = this.data.list;
    let row = list[id];
    if (row.logisticsInfo != undefined){
      if (row.logisticsInfo.logistics_no != undefined && row.logisticsInfo.logistics_no != null && row.logisticsInfo.logistics_no != ''){
        let logisticsUrl = "https://m.kuaidi100.com/index_all.html?type=" + row.logisticsInfo.logistics_company_code 
                          +"&postid=" + row.logisticsInfo.logistics_no;
        console.log(logisticsUrl);
        wx.setStorageSync("logisticsUrl", logisticsUrl);
        wx.navigateTo({ url: '../logistics/logistics',})
      }
    } else {
      wx.showModal({ title: '提示', content: '此单暂无物流信息', })
    }
  },
  //支付
  pay: function (e) {
    let orderNo = e.currentTarget.id;
    let openId = wx.getStorageSync("openidKey");
    let that = this;
    let params = new Object();
    params.orderNo = orderNo;
    params.openid = openId;
    console.log(JSON.stringify(params));
    commonUtil.showLoading("去支付...");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/order/payParam",//统一下单返回支付参数
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        console.log("统一下单返回" + JSON.stringify(res));
        res = res.data;
        if (res.result == 0) {
          var data = res.data;
          //调起支付
          wx.requestPayment(
            {
              'timeStamp': data.timeStamp,
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': 'MD5',
              'paySign': data.paySign,
              'success': function (res) {
                console.log("支付调起成功");
                that.setData({ pageNumber: 1 })
                that.list();
              },
              'fail': function (res) {
                console.log("用户取消支付");
              },
              'complete': function (res) { }
            })
        } else {
          commonUtil.showModal("支付失败：" + res.msg);
        }
      }
    })
  },
  onLoad: function (options) {
    // this.list();
  },

  onShow: function () {
    this.setData({ pageNumber:1})
    this.list();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNumber = this.data.pageNumber;
    pageNumber = pageNumber + 1;
    if (pageNumber > this.data.totalPage) {
      commonUtil.showSuccessToast("没有更多啦~");
      return;
    } else {
      this.setData({ pageNumber: pageNumber })
      this.list();
    }
  },
})